package cn.com.xy.sms.sdk.db.entity;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.db.base.BaseManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class EmergencyManger {
	


	public static final String TABLE_NAME="tb_emergency_queue";
	public static String getCreateTableSql(){
		return " create table  if not exists "  + TABLE_NAME + " ("+
				" id INTEGER PRIMARY KEY,"+			
				" emVersion INTEGER,"+			
				" emContent TEXT )";				
	}
		
	public static JSONObject queryNextEmergency(){

	   	 XyCursor cursor = null;
	   	 JSONObject jsonObject=null;
	        try {
	         
	           String[] fieldArr = new String[] { "id", "emVersion",
	                   "emContent"};
	            cursor = DBManager.query(TABLE_NAME, fieldArr, 
	                    null, null, null, null," emVersion asc","1");
	            jsonObject= BaseManager.loadSingleDataFromCursor(fieldArr, cursor);
	        } catch (Throwable e) {
	            // TODO Auto-generated catch block
//	            e.printStackTrace();
	            /*SDK-550 hewengao 20160719 start*/
	            LogManager.w(Constant.TAG, "queryNextEmergency: ", e);
	            /*SDK-550 hewengao 20160719 end*/
	        } finally {
	             XyCursor.closeCursor(cursor, true);
	        }	       
			return jsonObject;
	}	

    public static void insertOrUpdateEmergency(JSONArray emergencyArray) {
        try {
            if (emergencyArray == null || emergencyArray.length() <= 0)
                return;
            for (int i = 0; i < emergencyArray.length(); i++) {
                JSONObject jsonObject = emergencyArray.optJSONObject(i);
                insertOrUpdateEmergency(jsonObject);
            }

        } catch (Throwable e) {
            /* SDK-550 hewengao 20160719 start */
            LogManager.w(Constant.TAG, "insertOrUpdateEmergency ", e);
            /* SDK-550 hewengao 20160719 end */
        }

    }
	
    public static void insertOrUpdateEmergency(JSONObject jsonObject) {
        XyCursor xyCursor = null;
        try {
            if (jsonObject == null)
                return;
            String emContent = (String) JsonUtil.getValueFromJsonObject(jsonObject, "emContent");
            String emVersion = (String) JsonUtil.getValueFromJsonObject(jsonObject, "emVersion");
            if (StringUtils.isNull(emVersion) || StringUtils.isNull(emContent))
                return;

            xyCursor = DBManager.query(TABLE_NAME, new String[] { "emVersion" }, "emVersion = ?",
                    new String[] { emVersion + "" });
            ContentValues values = new ContentValues();
            values.put("emVersion", emVersion);
            values.put("emContent", emContent);
            if (xyCursor != null && xyCursor.getCount() > 0) {
                DBManager.update(TABLE_NAME, values, "emVersion = ? ", new String[] { emVersion + "" });
            } else {
                DBManager.insert(TABLE_NAME, values);
            }

        } catch (Throwable e) {
            // e.printStackTrace();
            /* SDK-550 hewengao 20160719 start */
            LogManager.w(Constant.TAG, "insertOrUpdateEmergency ", e);
            /* SDK-550 hewengao 20160719 end */
        } finally {
            XyCursor.closeCursor(xyCursor, true);

        }
    }
	
	public static void deleteEmergency(JSONObject jsonObject) {
		try {
			if (jsonObject == null) return;
			String emVersion = jsonObject.optString("emVersion");
			if (StringUtils.isNull(emVersion)) {
				emVersion = "";
			}
			DBManager.delete(TABLE_NAME, "emVersion = ?",
					new String[]{emVersion});
		} catch (Throwable e) {
			// TODO: handle Throwable
		}
	}
}
